//单向非循环无头节点
public class MyLinkedList implements SingleLinkedList {

    public Node head;
    @Override
    public void addFirst(int data) {
        Node node = new Node(data);
        node.next = head;
        head = node;
    }

    @Override
    public void addLast(int data) {
        if (head == null) {
            Node node = new Node(data);
            head = node;
            return;
        }
        Node cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        Node node = new Node(data);
        cur.next = node;
    }

    @Override
    public void addIndex(int index, int data) {
        int range = size();
        if (index > range || index < 0) {
            System.out.println("index不合法！！！");
            return;
        }
        Node node = new Node(data);
        Node cur = head;
        //头插
        if (index == 0) {
            addFirst(data);
            return;
        }
        //尾插
        if (index == range) {
            addLast(data);
            return;
        }
        //中间
        while ( index-1 != 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur.next;
        cur.next = node;
    }

    @Override
    public boolean contains(int key) {
        Node cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        Node cur = head;
        //如果head的val等于key
        if (cur.val == key) {
            head = cur.next;
            return;
        }
        while (cur.next != null) {
            if (cur.next.val == key) {
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        Node pre = head;
        Node cur = pre.next;
        while (cur != null) {
            if (cur.val == key) {
                pre.next = cur.next;
                cur = cur.next;
            } else {
                pre = cur;
                cur = cur.next;
            }
        }
        //如果头节点等于key（此代码必须放在最后）
        if (head.val == key) {
            head = head.next;
        }
    }

    @Override
    public int size() {
        int size = 0;
        Node cur = head;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    @Override
    public void clear() {
        Node cur = head;
        Node curCord = null;
        while (cur != null) {
            curCord = cur.next;
            cur.next = null;
            cur = curCord;
        }
        head = null;//头节点
    }

    @Override
    public void display() {
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void display(Node head) {
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void reverseLinkedList() {
        if (head.next == null) {
            return;
        }
        Node cur = head.next;
        Node curN = null;
        head.next = null;
        while (cur != null) {
            curN = cur.next;
            cur.next = head;
            head = cur;
            cur = curN;
        }
    }

    @Override
    public int findKthToLast(int k) {
        Node fast = head;
        Node slow = head;
        if (head == null) {
            return -1;
        }
        //先让fast走k-1步
        while (k-1 != 0) {
            fast = fast.next;
            if (fast == null) {
                return -1;
            }
            k--;
        }
        //fast和slow同时走
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }
        public Node partition(int x) {
            // write code here
            Node cur = head;
            Node bs = null;
            Node be = null;
            Node as = null;
            Node ae = null;
            if (head == null) {
                return null;
            }
            //划分区域
            while (cur != null) {
                if (cur.val < x) {
                    if (bs == null) {
                        bs = be = cur;
                    } else {
                        be.next = cur;
                        be = be.next;
                    }
                } else {
                    if (as == null) {
                        as = ae = cur;
                    } else {
                        ae.next = cur;
                        ae = ae.next;
                    }
                }
                cur = cur.next;
            }
            if (bs == null) {
                return as;
            }
            if (as != null) {
                ae.next = null;
            }
            //连接两个链表
            be.next = as;
            return bs;
        }

    @Override
    public boolean chkPalindrome() {
        Node slow = head;
        Node fast = head;
        //找到中间节点
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        Node cur = head.next;
        Node curN = cur;
        //从中间节点往后反转链表
        while (cur != null) {
            curN = curN.next;
            cur.next = slow;
            slow = cur;
            cur = curN;
        }
        //判断是否回文
        if (fast == null) {
            fast = slow;
        }
        slow = head;
        while (fast != slow) {
            //如果链表中有不相同的直接返回false
            if (fast.val != slow.val) {
                return false;
            }
            //当链表是奇数个节点时
            if (fast == slow) {
                return true;
            }
            //当链表有偶数个节点时
            if (slow.next == fast) {
                return true;
            }
            slow = slow.next;
            fast = fast.next;
        }
        return true;
    }
}
